import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '@/store'

Vue.use(VueRouter)

// const routes = [
//     {
//         path: '/login',
//         name: 'Login',
//         component: () => import('@/views/login/login'),
//         meta: {
//             keepalive: true
//         }
//     },
//     {path: '/register', component: () => import('@/views/login/register')},
//     {
//         path: '/hello',
//         name: 'Hello',
//         component: HelloWorld,
//         meta: {
//             keepalive: true
//         }
//     },
//     { path: '/index', component: () => import('@/components/Index'),
//         children: [
//             {
//                 path: 'system',
//                 children: [
//                     {path: 'user', component: () => import('@/views/system/user')}
//                 ]
//             },
//             { path: 'error', component: () => import('@/views/error'), hidden: true},
//             { path: 'success', component: () => import('@/views/success')},
//         ]
//     },
//     { path: '/temp', component: () => import('@/views/temp'),
//         children: [
//             { path: 'error', component: () => import('@/views/error'), hidden: true},
//             { path: 'success', component: () => import('@/views/success')},
//         ]
//     },
// ];
const routes = [
    {
        path: '/',
        component: () => import('@/App'),
        children: [
            {path: 'login', component: () => import('@/views/login/login')},
            {path: 'register', component: () => import('@/views/login/register')},
            {
                path: 'system',
                component: () => import('@/components/System'),
                children: [
                    {path: 'error', component: () => import('@/views/error'), hidden: true},
                    // 默认路由
                    {path: '', component: () => import('@/views/success')},
                    {path: 'user', component: () => import('@/views/system/user')},
                ]
            }
        ]
    }
]
const router = new VueRouter({
    scrollBehavior: () => ({
        y: 0
    }),
    routes: routes
});

var skipPath = ['/error', '/login', '/hello', '/register'];
router.beforeEach(((to, from, next) => {
    // debugger;
    if (skipPath.includes(to.path)) {
        next();
        return;
    }
    let token = store.getters['user/token'];
    if (token !== "") {
        next();
        return;
    }
    router.push({
        path: '/login'
    })


}))
export default router